# -*- coding: utf-8 -*-
"""
Created on Wed Sep 16 14:03:53 2020

@author: Administrator
"""

'''

Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

Each number in candidates may only be used once in the combination.

Note:

All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Example 1:

Input: candidates = [10,1,2,7,6,1,5], target = 8,
A solution set is:
[
  [1, 7],
  [1, 2, 5],
  [2, 6],
  [1, 1, 6]
]
Example 2:

Input: candidates = [2,5,2,1,2], target = 5,
A solution set is:
[
  [1,2,2],
  [5]
]
'''

class Solution:
    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
        
        '''
        20200916--李运辰
        '''
        
        if len(candidates)==0:
                return []
        
        candidates.sort()
        res=[]
        def helper(target,index,sub_list):
            if  target==0:
                
                '''增加部分   判断元素是否只用一次'''
                ca =[]
                for j in candidates:
                    ca.append(j)
                f=True
                for k in range(0,len(sub_list)):
                    if sub_list[k] in ca:
                        ca.remove(sub_list[k])
                    else:
                        f=False
                if f:
                    if sub_list not in res:
                        res.append(sub_list)
                '''增加部分end'''
            
            if target<0 or index>=len(candidates):
                 return
            
            for i in range(index,len(candidates)):
                if target < candidates[i]:
                    break
                helper(target-candidates[i],i,sub_list+[candidates[i]])
            
        helper(target,0,[])
        
        return res